package org.example.leetcode;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/05/13 18:40
 */
public class Solution503 {

    public static void main(String[] args) {

        Solution503 solution503 = new Solution503();
        int[] nums = {1,2,3,4,3,2};
        int[] next = solution503.nextGreaterElements(nums);
        for(int i=0;i<next.length;i++){
            System.out.println(next[i] + "");
        }

    }

    public int[] nextGreaterElements(int[] nums) {
        int[] next = new int[nums.length];
        int n = nums.length;
        int max = Integer.MIN_VALUE;
        for(int i=0;i<nums.length;i++){
            max = Math.max(max, nums[i]);
        }

        for(int i=0;i<nums.length;i++){
            if(nums[i] == max){
                next[i] = -1;
            }else {
                int j=i+1;
                while(j<i+1+n){
                    if(nums[j%n] > nums[i]){
                        next[i] = nums[j%n];
                        break;
                    }
                    j++;
                }
            }
        }

        return next;
    }

}
